package susebs.pb.travel_server.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import susebs.pb.travel_server.domain.HotelRoom;
import susebs.pb.travel_server.domain.PageDto;
import susebs.pb.travel_server.entity.Hotels;
import susebs.pb.travel_server.entity.Orders;
import susebs.pb.travel_server.entity.Pictures;
import susebs.pb.travel_server.entity.Rooms;
import susebs.pb.travel_server.dao.RoomsDao;
import susebs.pb.travel_server.service.HotelsService;
import susebs.pb.travel_server.service.RoomsService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 房间表(Rooms)表服务实现类
 *
 * @author makejava
 * @since 2024-02-05 08:02:28
 */
@Service("roomsService")
public class RoomsServiceImpl implements RoomsService {
    @Resource
    private RoomsDao roomsDao;
    @Resource
    private HotelsService hotelsService;

    /**
     * 通过ID查询单条数据
     *
     * @param roomId 主键
     * @return 实例对象
     */
    @Override
    public Rooms queryById(Long roomId) {
        return this.roomsDao.queryById(roomId);
    }

    /**
     * 分页查询
     *
     * @param rooms       筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<Rooms> queryByPage(Rooms rooms, PageRequest pageRequest) {
        long total = this.roomsDao.count(rooms);
        return new PageImpl<>(this.roomsDao.queryAllByLimit(rooms, pageRequest), pageRequest, total);
    }

    /**
     * 分页查询
     */
    @Override
    public PageInfo queryAllRoomsByPage(PageDto pageDto) {
        if(pageDto.getPage() == null || pageDto.getSize() == null){
            pageDto.setPage(1);
            pageDto.setSize(10);
        }
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        List<Rooms> list = roomsDao.queryAllRooms1(pageDto.getRooms());
        HotelRoom hotelRoom;
        ArrayList<HotelRoom> res = new ArrayList<>();
        // 查询房间对应的酒店名并完成封装
        for (Rooms r:list) {
            hotelRoom = new HotelRoom();
            String hotelName = hotelsService.queryById(r.getHotelId()).getHotelName();

            hotelRoom.setRooms(r);
            hotelRoom.setHotelName(hotelName);

            res.add(hotelRoom);
        }
        PageInfo<HotelRoom> pageInfo = new PageInfo<>(res);
        return pageInfo;
    }

    /**
     * 查询指定酒店的所有房间数据
     */
    @Override
    public List<Rooms> queryAllRooms(Long hotelId) {
        List<Rooms> res = roomsDao.queryAllRooms(hotelId);
        return res;
    }

    /**
     * 新增数据
     *
     * @param rooms 实例对象
     * @return 实例对象
     */
    @Override
    public Rooms insert(Rooms rooms) {
        this.roomsDao.insert(rooms);
        return rooms;
    }

    /**
     * 修改数据
     *
     * @param rooms 实例对象
     * @return 实例对象
     */
    @Override
    public Rooms update(Rooms rooms) {
        this.roomsDao.update(rooms);
        return this.queryById(rooms.getRoomId());
    }

    /**
     * 通过主键删除数据
     *
     * @param roomId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long roomId) {
        return this.roomsDao.deleteById(roomId) > 0;
    }
}
